home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
10,000 Great Games
/
10,000 Great Games.iso
/
Product
/
66
/
data1.cab
/
Source_Files
/
Src
/
Wave.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
2000-01-16
|
3KB
|
157 lines
#include "stdafx.h"
#define WAV_DATA_OFFSET 44
#define WAV_FMT_OFFSET 20
cWAV *wave = 0;
cWAV::cWAV(LPDIRECTSOUNDBUFFER _dsb, const char *_name)
{
dsb = _dsb;
name = strdup(_name);
add_end((cList **)&wave);
}
cWAV::~cWAV()
{
free(name);
dsb->Release();
}
static void create_add_file(const char *fn, const char *name)
{
// Read the wave file
int size;
char *d = read_file(fn, &size);
// Open database file
CFile f;
if (!f.Open("Wave.dat", CFile::modeCreate | CFile::modeNoTruncate | CFile::modeWrite | CFile::shareExclusive))
error("Unable to open Wave.dat");
f.SeekToEnd();
// Write name of object + bytecount of string
int l = strlen(name);
f.Write(&l, sizeof(l));
f.Write(name, l);
// Write WAVEFORMATEX block
f.Write(d + WAV_FMT_OFFSET, sizeof(WAVEFORMATEX));
// Write lenght of data
size -= WAV_DATA_OFFSET;
f.Write(&size, sizeof(size));
// Write data
f.Write(d + WAV_DATA_OFFSET, size);
// Release temporary space
delete d;
}
void cWAV::create_file()
{
search_files("Wav", "*.WAV", create_add_file);
}
void cWAV::load()
{
LPDIRECTSOUNDBUFFER dsb;
DSBUFFERDESC dsbd;
WAVEFORMATEX wf;
char name[256], *d;
unsigned long s;
int size, l;
void *ptr;
CFile f;
if (no_sound)
return;
// Open wave file
if (!f.Open("Wave.dat", CFile::modeRead | CFile::shareDenyWrite))
error("Unable to open Wave.dat");
while (f.Read(&l, sizeof(l)) == sizeof(l))
{
// Read name
f.Read(name, l);
name[l] = 0;
// Read WAVEFORMATEX block
f.Read(&wf, sizeof(WAVEFORMATEX));
// Read size
f.Read(&size, sizeof(size));
// Create temporary buffer
d = new char [size];
// Read wave data
f.Read(d, size);
// Create sound buffer
ZeroMemory(&dsbd, sizeof(dsbd));
dsbd.dwSize = sizeof(dsbd);
dsbd.dwFlags = DSBCAPS_CTRLFREQUENCY | DSBCAPS_CTRLPAN | DSBCAPS_CTRLVOLUME | DSBCAPS_STATIC;
dsbd.dwBufferBytes = size;
dsbd.lpwfxFormat = &wf;
if (FAILED(DS->CreateSoundBuffer(&dsbd, &dsb, 0)))
error("Unable to create sound buffer");
// Lock the buffer
if (FAILED(dsb->Lock(0, size, &ptr, &s, 0, 0, 0)))
error("Unable to lock sound buffer");
// Copy data in the buffer
memcpy(ptr, d, s);
// Unlock the buffer
if (FAILED(dsb->Unlock(ptr, size, 0, 0)))
error("Unable to unlock sound buffer");
// Release temporary memory
delete d;
// Add to list
new cWAV(dsb, name);
}
}
cWAV *cWAV::get(char *fn)
{
if (!no_sound)
{
for (cWAV *b = wave; b != 0; b = (cWAV *)b->next)
if (eq(fn, b->name))
return b;
error("Wave file %s not found", fn);
}
return 0;
}